/**  
*  Doku2Buddy - A DokuWiki to BuddyPress Wiki-Component Converter
*  Copyright (C) 2010 Skye Book
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*  
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.sbook.d2b.formatting;

/**
 * @author Skye Book
 *
 */
public class SyntaxEditorCodeConverter implements CodeConverter {

	/* (non-Javadoc)
	 * @see com.sbook.d2b.formatting.CodeConverter#convertCode(java.lang.String)
	 */
	@Override
	public String convertCode(DokuArticle article){
		
		// Get the raw article
		// Escape the quotes
		// swap < for [ and > for ]
		
		System.out.println("Converting code in " + article.getTitle());
		String newArticle="";
		String htmlMarkup = article.getMarkup();
		String rawMarkup = article.getOriginalMarkup();
		String foundFormat=null;
		while((foundFormat=hasMoreCode(htmlMarkup))!=null){
			int htmlCodeOpen = htmlMarkup.indexOf("<pre class=\"code "+foundFormat);
			// add 6 here so we will go to the *end* of the </pre> tag
			int htmlCodeClose = htmlMarkup.indexOf("</pre>")+6;
			
			// add everything up to the beginning of the code
			newArticle+=htmlMarkup.substring(0, htmlCodeOpen);
			
			// cut down the doku article in preparation for the next pass
			htmlMarkup = htmlMarkup.substring(htmlCodeClose, htmlMarkup.length());
			
			// find the syntax from the original wiki file
			int startOld = rawMarkup.indexOf("<code");
			int endOld = rawMarkup.indexOf("</code>");
			
			String dokuCodeBlock = rawMarkup.substring(startOld, endOld+7);
			System.out.println(dokuCodeBlock);
			
			String openingTag=dokuCodeBlock.substring(0, dokuCodeBlock.indexOf(">")+1);
			System.out.println("opening tag: " + openingTag);
			
			String closingTag="</code>";
			
			dokuCodeBlock = dokuCodeBlock.replace(openingTag, "["+foundFormat+"]");
			dokuCodeBlock = dokuCodeBlock.replace(closingTag, "[/"+foundFormat+"]");
			
			// escape quotes:
			dokuCodeBlock = dokuCodeBlock.replaceAll("\"", "\\\\\"");
			
			System.out.println("Whole Block:\n"+dokuCodeBlock);
			
			// add the syntax to the new file
			newArticle+=dokuCodeBlock;
			// remove the syntax from the original markup
			rawMarkup = rawMarkup.substring(endOld+7, rawMarkup.length());
		}
		
		// add any leftover markup to the file
		newArticle+=htmlMarkup;
		
		return newArticle;
	}
	
	public String hasMoreCode(String toTest){
		if(toTest.contains("<pre class=\"code java")){
			System.out.println("Java code found");
			return "java";
		}
		else if(toTest.contains("<pre class=\"code xml")){
			System.out.println("XML code found");
			return "xml";
		}
		else if(toTest.contains("<pre class=\"code php")){
			System.out.println("PHP code found");
			return "php";
		}
		else if(toTest.contains("<pre class=\"code")){
			System.out.println("Language Undetected");
			return "code";
		}
		else{
			System.out.println("no code found");
			return null;
		}
	}
}
